/*	This file is part of AstroAttack.
*
*	AstroAttack is free software; you can redistribute it and/or modify
*	it under the terms of the GNU General Public License as published by
*	the Free Software Foundation; either version 2 of the License, or
*	(at your option) any later version.
*	
*	AstroAttack is distributed in the hope that it will be useful,
*	but WITHOUT ANY WARRANTY; without even the implied warranty of
*	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
*	GNU General Public License for more details.
*	
*	You should have received a copy of the GNU General Public License
*	along with AstroAttack; if not, write to the Free Software
*	Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
*	
*	$Id: BulletController.java 64 2007-01-06 03:30:23Z xdracox $
*/

/**	The AstroAttack package contains all the classes that belong to the
*	AstroAttack program.
*/
package AstroAttack;

import java.util.Iterator;
import java.util.ArrayList;
import java.awt.Point;

// BulletController is a singleton class that moves all the Bullets
// that are flying around the screen
public class BulletController implements Controller
{
	/**	The single instance of the class. */
	private static BulletController m_ref;
	
	/**	This method returns the instance or initializes if it hasn't been
	*	initialized.
	*	@return	the instance of BulletController
	*/
	public static BulletController getBulletController()
	{
		if ( m_ref == null )
			m_ref = new BulletController();
		return m_ref;
	}
	
	private ArrayList<Bullet> goodBullets, badBullets;
	private AlienController m_ac;
	private ShipController m_sc;
	
	private BulletController()
	{
		m_ref = this;
		m_ac = AlienController.getAlienController();
		m_sc = ShipController.getShipController();
		goodBullets = new ArrayList<Bullet>();
		badBullets = new ArrayList<Bullet>();
	}
	
	public synchronized void control()
	{
		Iterator<Bullet> iter = getEntities().iterator();
		
		while ( iter.hasNext() )
		{
			Bullet e = iter.next();
			
			if(!e.isAlive())
			{
				iter.remove();
				continue;
			}
			e.move();
		}
		iter = goodBullets.iterator();
		Iterator<Alien> eiter;
		boolean removed;
		while ( iter.hasNext() )
		{
			Bullet b = iter.next();
			eiter = m_ac.getEntities().iterator();
			removed = false;
			while ( eiter.hasNext() )
			{
				Entity a = eiter.next();
				if(!removed && a.getBounds().intersects(b.getBounds()))
				{
					a.shot(b.getDamage());
					iter.remove();
					removed = true;
				}
			}
		}
		iter = badBullets.iterator();
		Ship ship = Ship.getShip();
		while ( iter.hasNext() )
		{
			Bullet b = iter.next();
			if(ship.getBounds().intersects(b.getBounds()))
			{
				ship.shot(b.getDamage());
				iter.remove();
			}
		}
	}
	
	public synchronized void addBullet(Bullet b, int team)
	{
		if ( team == 1 )
			goodBullets.add(b);
		else
			badBullets.add(b);
	}
	
	/*public synchronized void newBullet(BulletType bt, int x, int y, int dir)
	{
		Bullet temp = new Bullet(bt, new Point(x, y), dir);
		if(temp.getTeam()==1)
			goodBullets.add(temp);
		else
			badBullets.add(temp);
	}*/
	
	public synchronized ArrayList<Bullet> getEntities()
	{
		ArrayList<Bullet> bullets = new ArrayList<Bullet>();
		bullets.addAll(goodBullets);
		bullets.addAll(badBullets);
		return bullets;
	}
}